from django.http import HttpResponse
from django.shortcuts import render
from django.forms import widgets
from .utils import createAndConnectDB as db


class Form:
    def __init__(self, mid, name, sign, level):
        self.mid = mid
        self.name = name
        self.sign = sign
        self.level = level


class Forms:
    def __init__(self, user_list):
        length = len(user_list)
        f = Form
        self.form = [f for i in range(length)]
        for i in range(0, length):
            l = user_list[i]
            self.form[i] = Form(l[1], l[2], l[3], l[4])

def index(request):
    context = {}
    # context['hello'] = 'Hello World!'
    if request.method == "GET":
        mid = request.GET.get('mid', default='')
        level = request.GET.get('level', default='')

        if mid != '':
            Type = 'mid'
            lists = db.select(Type, mid)
            forms = Forms(lists)
            return render(request, 'form.html', locals())
        elif level != '':
            Type = 'level'
            lists = db.select(Type, level)
            forms = Forms(lists)
            return render(request, 'form.html', locals())
        else:
            return HttpResponse("ERROR")
    return render(request, 'index.html', locals())
